/*
 * Copyright 2004 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.extremecomponents.table.view.html;

import org.apache.commons.lang.StringUtils;
import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.bean.EccnColumnDefaults;
import org.extremecomponents.table.core.EccnTableConstants;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.html.BuilderConstants;
import org.extremecomponents.table.view.html.ColumnBuilder;
import org.extremecomponents.util.HtmlBuilder;

/**
 * @author guanhw
 */
public class EccnColumnBuilder extends ColumnBuilder {
	private TableModel model;
	
	public EccnColumnBuilder(TableModel model, Column column) {
        this(column );
		this.model = model;
    }
	
	public EccnColumnBuilder(TableModel model, HtmlBuilder html, Column column) {
        this(html, column );
		this.model = model;
    }
	
	public EccnColumnBuilder(Column column) {
        this(new HtmlBuilder(), column);
    }

    public EccnColumnBuilder(HtmlBuilder html, Column column) {
    	super(html, column);
    }
    
    public void tdStart() {
        getHtmlBuilder().td(2);
        styleClass();
        style();
        width();
        onclick();
        onmouseover();
        onmouseout();
        getHtmlBuilder().close();
    }
    
    public void onclick() {
        String onclick = getColumn().getAttributeAsString(EccnTableConstants.ON_CLICK);
        getHtmlBuilder().onclick(onclick);
    }
    
    public void onmouseover() {
    	Boolean highlight = Boolean.valueOf(getColumn().getAttributeAsString(EccnTableConstants.HIGHLIGHT));
        highlight = EccnColumnDefaults.isHighlightRow(model,highlight);
        if (highlight.booleanValue()) {
            String highlightClass = getColumn().getAttributeAsString(EccnTableConstants.HIGHLIGHT_CLASS);
            highlightClass = EccnColumnDefaults.getHighlightClass(model,highlightClass);
            if (StringUtils.isNotBlank(getColumn().getAttributeAsString(EccnTableConstants.ON_MOUSE_OVER))) {
            	getHtmlBuilder().onmouseover("this.className='" + highlightClass + "';" + getColumn().getAttributeAsString(EccnTableConstants.ON_MOUSE_OVER));
            } else {
            	getHtmlBuilder().onmouseover("this.className='" + highlightClass + "'");
            }
        } else {
        	getHtmlBuilder().onmouseover(getColumn().getAttributeAsString(EccnTableConstants.ON_MOUSE_OVER));
        }
    }

    public void onmouseout() {
    	Boolean highlight = Boolean.valueOf(getColumn().getAttributeAsString(EccnTableConstants.HIGHLIGHT));
        highlight = EccnColumnDefaults.isHighlightRow(model,highlight);
        if (highlight.booleanValue()) {
            String styleClass = getStyleClass();
            if (StringUtils.isNotBlank(getColumn().getAttributeAsString(EccnTableConstants.ON_MOUSE_OUT))) {
            	getHtmlBuilder().onmouseout("this.className='" + styleClass + "';" + getColumn().getAttributeAsString(EccnTableConstants.ON_MOUSE_OUT));
            } else {
            	getHtmlBuilder().onmouseout("this.className='" + styleClass + "'");
            }
        } else {
        	getHtmlBuilder().onmouseout(getColumn().getAttributeAsString(EccnTableConstants.ON_MOUSE_OUT));
        }
    }
    
    protected String getStyleClass() {
        String styleClass = getColumn().getStyleClass();
        if (StringUtils.isNotBlank(styleClass)) {
            return styleClass;
        }

        if (model.getRowHandler().isRowEven()) {
            return BuilderConstants.ROW_EVEN_CSS;
        }

        return BuilderConstants.ROW_ODD_CSS;
    }
}
